<template>
  <div class="messageCenter">
    <h2 class="title"><span>我的消息</span>
      <el-button type="primary" class="btn" @click="changeAll">全部标为已读</el-button>
    </h2>
    <div class="messageContent">
      <ul v-if='data.length' class="page">
        <li v-for='item in data' :key='item.id' class="list">
          <div class="system">
            <span class="message">系统消息</span>
            <span>{{item.createTime}}</span>
          </div>
          <div class="info">
            <span class="feedback">{{item.content}}</span>
            <span class="del" @click="()=>{del(item.id)}">删除</span>
          </div>
        </li>
      </ul>
      <div v-else class="list">暂无数据</div>
      <el-pagination background @current-change="handleCurrentChange" :page-size="size" :current-page="page"
        layout="prev, pager, next" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { msgListPage, msgDelete, changeAllMsg } from '../../../api/messageCenter'
import bus from '../../common/bus'
export default {
  data () {
    return {
      page: 1,
      total: 0,
      size: 10,
      data: []
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 全部标记已读
    changeAll () {
      changeAllMsg().then(res => {
        if (res.success) {
          this.$message.success('标记成功！')
          this.getList()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 下一页
    handleCurrentChange (val) {
      this.page = val;
      this.getList()
    },
    // 删除消息
    del (val) {
      this.$confirm('您要删除系统消息，删除后不可恢复，是否确定操作？', '信息提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        lockScroll: false,
        type: 'warning',
        callback: action => {
          console.log(action);
          if (action === 'confirm') {
            msgDelete(val).then(res => {
              if (res.success) {
                this.$message.success('删除成功')
                this.getList()
              } else {
                this.$$message.error(`${res.msg}`)
              }
            })
          }
        }
      });
    },
    // 获取消息列表
    getList () {
      let formData = new FormData();
      formData.append('page', this.page);
      formData.append('size', this.size);
      formData.append('sort',JSON.stringify([{ column: 'create_time', type: 'desc' }]))
      msgListPage(formData).then(res => {
        console.log(res);
        if (res.success) {
          this.data = res.data.records || [];
          this.total = res.data.total;
          bus.$emit('checkMsg')
        } else {
          this.$message.error(`${res.msg}`)
        }
      })
    }
  }
}
</script>

<style scoped>
.messageCenter {
    width: 1200px;
    background: rgba(255, 255, 255, 1);
    opacity: 1;
}
.messageCenter .title {
    width: 1200px;
    padding: 28px 44px 13px;
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 600;
    color: rgba(34, 34, 34, 1);
    border-bottom: 1px solid rgba(232, 232, 232, 1);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    box-sizing: border-box;
    opacity: 1;
}
.messageCenter .messageContent {
    padding: 31px 44px 49px;
    box-sizing: border-box;
}
.messageCenter .nav {
    margin-bottom: 14px;
}
.messageCenter .list {
    width: 1112px;
    text-align: center;
    margin-bottom: 8px;
    line-height: 89px;
    background: rgba(255, 255, 255, 1);
    border: 2px solid rgba(248, 248, 248, 1);
    opacity: 1;
}
.messageCenter .page .list {
    padding: 19px 40px;
    box-sizing: border-box;
}
.messageCenter .page .list .info {
    display: flex;
    justify-content: space-between;
    /* height: 24px; */
    /* line-height: 24px; */
}
.messageCenter .page .list .info .feedback {
    /* height: 24px; */
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 24px;
    text-align: left;
    color: rgba(85, 85, 85, 1);
    opacity: 1;
}
.messageCenter .page .list .info .del {
    /* display: inline-block; */
    cursor: pointer;
    width: 94px;
    height: 24px;
    font-size: 12px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 24px;
    color: rgba(255, 255, 255, 1);
    background: rgba(24, 147, 252, 1);
    opacity: 1;
}
.messageCenter .page .list .system {
    height: 22px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
}
.messageCenter .page .list .system span {
    height: 22px;
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 22px;
    color: rgba(85, 85, 85, 1);
    opacity: 1;
}
.messageCenter .page .list .system .message {
    color: rgba(51, 51, 51, 1);
}
.messageCenter .nav span {
    display: inline-block;
    padding: 0 10px;
    height: 24px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 24px;
    color: rgba(85, 85, 85, 1);
    opacity: 1;
    cursor: pointer;
}
.messageCenter .nav span.selected {
    height: 24px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 24px;
    padding: 0 10px;
    color: rgba(255, 255, 255, 1);
    height: 24px;
    background: rgba(24, 147, 252, 1);
    opacity: 1;
}
.messageCenter .el-pagination {
    margin-top: 41px;
    text-align: center;
}
</style>